package com.xuanyuan.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView


/**
 * @author tanzh
 */
abstract class BaseListAdapter<T>(private var multiType : MultipleType<T>,diffCallback : DiffUtil.ItemCallback<T>) : ListAdapter<T,BaseListAdapter.BaseHolder2<T>>(diffCallback) {
    override fun onCreateViewHolder(parent : ViewGroup,viewType : Int) : BaseHolder2<T> {
        val itemView : View = LayoutInflater.from(parent.context).inflate(multiType.getTypeLayoutId(viewType),parent,false)
        return BaseHolder2(itemView)
    }
    
    override fun onBindViewHolder(holder : BaseHolder2<T>,position : Int) {
        bindUi(holder.itemView,getItem(position),getItemViewType(position))
    }
    
    override fun getItemViewType(position : Int) : Int {
        return multiType.getItemType(position,getItem(position))
    }
    
    /**
     * var binding : ViewDataBinding? = null
     * binding = DataBindingUtil.bind(holder.itemView)
     */
    abstract fun bindUi(itemView : View,t : T,viewType : Int)
    
    /**
     * 创建viewHolder
     */
    //    protected abstract fun createBaseViewHolder(parent : ViewGroup?,layoutId : Int) : BaseHolder2<T>
    class BaseHolder2<T>(itemView : View) : RecyclerView.ViewHolder(itemView) {
        //        var mBinding : ViewDataBinding? = DataBindingUtil.bind(itemView)
//        var data : T? = null
//
//        fun setData(t : T) {
//            data = t
//        }
    }
    
    /**
     * @author tanzh
     */
    interface MultipleType<T> {
        /**
         * 获取具体layoutId
         *
         * @param itemType 类型
         * @return  layout  id
         */
        fun getTypeLayoutId(itemType : Int) : Int
        
        /**
         * 获取具体的类型
         *
         * @param position  位置
         * @param t   类型
         * @return    layout 类型
         */
        fun getItemType(position : Int,t : T) : Int
    }
    
    /**
     * 类型
     */
    interface ItemType {
        companion object {
            const val ITEM_TYPE_NORMAL : Int = 0
            const val ITEM_TYPE_EMPTY : Int = 1
            const val ITEM_TYPE_SEEK : Int = 2
        }
    }
    
}
